﻿using System;
using NUnit.Framework;
using System.Linq;
using SilentVoice.Models;
using System.Data.Entity.Validation;
using System.Collections.Generic;

namespace SilentVoice.Data.Tests
{
    [TestFixture]
    public class EventManagementTests
    {
        [TestFixtureSetUp]
        public void Setup()
        {
            TestData.Create();
        }

        [TestFixtureTearDown]
        public void TearDown()
        {
            TestData.Wipe();
        }

        [Test]
        public void BasicEventCRUD()
        {
            Event scienceClass = new Event(){
                Name = "Wilkins Lecture 1",
                Description = "Going over acids and bases",
                AccountId = 1,
                OwnerId = 1,
                Outline = ""
            };

            EventManagement eventManager = new EventManagement();
            
            //Create
            Event createdEvent = eventManager.CreateEvent(scienceClass);
            Assert.NotNull(createdEvent, "Created event was null.");
            Assert.That(scienceClass.Equals(createdEvent),
                "Created event was not the same as what was passed in");

            //Read
            Event historyForum = eventManager.GetEvent(2);
            Assert.NotNull(historyForum, "Existing event retrieved was null.");
            Assert.That(historyForum.Equals(scienceClass),
                "Retrieved event was not the same as what was inserted.");
            
            //Update
            createdEvent.Description = "Suspended until tomorrow";
            Event updatedEvent = eventManager.UpdateEvent(createdEvent);
            Assert.NotNull(updatedEvent, "Updated event was null.");
            Assert.That(updatedEvent.Description.Equals(createdEvent.Description), "Event was not updated");

            //Check updated
            Event updatedScienceClass = eventManager.GetEvent(2);
            Assert.NotNull(updatedScienceClass, "Updated event retrieved was null.");
            Assert.That(createdEvent.Equals(updatedScienceClass),
                "Event was actually never updated in database. Updated: {0} | Retrieved: {1}", createdEvent, updatedScienceClass);

            //Delete
            Event deletedEvent = eventManager.DeleteEvent(createdEvent);
            Assert.NotNull(deletedEvent, "Deleted event retrieved was null.");

            //Check deleted
            Event checkDeleted = eventManager.GetEvent(2);
            Assert.Null(checkDeleted, "Event was never deleted");
            
        }

        [Test]
        public void BasicScratchPadCRUD()
        {
            ScratchPad scienceJournal = new ScratchPad()
            {
                EventId = 1,
                Notes = "It was an awesome lecture. Had tons of fun.",
                OwnerId = 2
            };

            EventManagement eventManager = new EventManagement();

            //Create
            ScratchPad createdScratchPad = eventManager.CreateScratchPad(scienceJournal);
            Assert.NotNull(createdScratchPad, "Created scratchpad was null.");
            Assert.That(scienceJournal.Equals(createdScratchPad),
                "Created scratchpad was not the same as what was passed in");

            //Read
            ScratchPad historyForum = eventManager.GetScratchPad(2);
            Assert.NotNull(historyForum, "Existing scratchpad retrieved was null.");
            Assert.That(historyForum.Equals(scienceJournal),
                "Retrieved scratchpad was not the same as what was inserted.");

            //Update
            createdScratchPad.Notes = "It was an awesome lecture. Had tons of fun. Hope this was all worth it.";
            ScratchPad updatedScratchPad = eventManager.UpdateScratchPad(createdScratchPad);
            Assert.NotNull(updatedScratchPad, "Updated scratchpad was null.");
            Assert.That(updatedScratchPad.Notes.Equals(createdScratchPad.Notes), "Event was not updated");

            //Check updated
            ScratchPad updatedScratch = eventManager.GetScratchPad(2);
            Assert.NotNull(updatedScratch, "Updated scratchpad retrieved was null.");
            Assert.That(createdScratchPad.Equals(updatedScratch),
                "Scratchpad was actually never updated in database. Updated: {0} | Retrieved: {1}", createdScratchPad, updatedScratch);

            //Delete
            ScratchPad deletedScratchPad = eventManager.DeleteScratchPad(createdScratchPad);
            Assert.NotNull(deletedScratchPad, "Deleted scratchpad retrieved was null.");

            //Check deleted
            ScratchPad checkDeleted = eventManager.GetScratchPad(2);
            Assert.Null(checkDeleted, "Scratchpad was never deleted");
        }

        [Test]
        public void ManageEventGroups()
        {
            EventManagement eventManager = new EventManagement();

            Event historyForum = eventManager.GetEvent(1);

            //Read
            ICollection<Group> allowedGroups = eventManager.GetEventAllowedGroups(historyForum);
            Assert.NotNull(allowedGroups, "Event's allowed groups was null. It should have had at least one group.");
            Assert.That(allowedGroups.SingleOrDefault(x => x.Id == 1) != null, "Event should have allowed our first group.");

            allowedGroups.ToList().ForEach(x=> System.Console.WriteLine(x.Name));

            //Allow
            ICollection<Group> newGroups = eventManager.AllowGroup(historyForum, new Group() {Id=2});
            Assert.NotNull(newGroups, "Event's allowed groups was null. It should have had at least one group.");
            Assert.That(newGroups.SingleOrDefault(x => x.Id == 1) != null, "Event should have allowed our first group.");
            Assert.That(newGroups.SingleOrDefault(x => x.Id == 2) != null, "Event should have allowed our newly created group.");

            //Check allow
            ICollection<Group> postNewGroups = eventManager.GetEventAllowedGroups(historyForum);
            Assert.NotNull(postNewGroups, "Event's allowed groups was null. It should have had at least one group.");
            Assert.That(postNewGroups.SingleOrDefault(x => x.Id == 1) != null, "Event should have allowed our first group.");
            Assert.That(postNewGroups.SingleOrDefault(x => x.Id == 2) != null, "Event did not actually allow our newly created group.");

            //Remove
            ICollection<Group> oldGroups = eventManager.DenyGroup(historyForum, new Group() { Id = 2 });
            Assert.NotNull(oldGroups, "Event's allowed groups was null. It should have had at least one group.");
            Assert.That(oldGroups.SingleOrDefault(x => x.Id == 1) != null, "Event should have allowed our first group.");
            Assert.That(oldGroups.SingleOrDefault(x => x.Id == 2) == null, "Event should not have allowed our newly created group.");

            //Check deny
            ICollection<Group> postOldGroups = eventManager.GetEventAllowedGroups(historyForum);
            Assert.NotNull(postOldGroups, "Event's allowed groups was null. It should have had at least one group.");
            Assert.That(postOldGroups.SingleOrDefault(x => x.Id == 1) != null, "Event should have allowed our first group.");
            Assert.That(postOldGroups.SingleOrDefault(x => x.Id == 2) == null, "Event did not actually deny our newly created group.");
        }
    }
}
